/* todo

- what if there is no connection for uri, and two clients try to make one at same time? both will initiate a websocket. there should be some kind of flag and queue after requesting a new connection so that subsequent requests are queued and notified.

- store only websocket connections? not ok, since there needs to be a single data processing handler.

*/
var testerHandler = {};

testerHandler.handleRequest = function(request, response) {};
testerHandler.onConnect = function(success, path, realPrefix, registrationKey)
testerHandler.onConnect

var revHttp = function(){
  var revHttpProxies = {};
  
  function connect(revHttpProxyUri, onConnectHandler) {
    if (typeof revHttpProxies[revHttpProxyUri] !== 'undefined') {
      onConnectHandler(revHttpProxies[revHttpProxyUri]);
    } else {
      var newProxyConnection = function() {
        var handlers = {};
        var ws = WebSocket(revHttpProxyUri);
        var connected = false;
        
        ws.onopen = function() {
          connected = true;
          revHttpProxies[revHttpProxyUri] = newProxyConnection;
          onConnectHandler(revHttpProxies[revHttpProxyUri]);
        };
        
        ws.onclose = function() {
          if (!connected) {
            return;
          } else {
            if (typeof newProxyConnection.onclose !== 'undefined') {
              newProxyConnection.onclose(); // multiple handlers
            }
          }
          
          delete revHttpProxies[revHttpProxyUri];
        };
        
        function close() {
          
        };
        
        ws.ondata = function(data) {
          // if response to request - find request in outgoing queue
          // else discard
          // if request - forward to handler and add to incoming queue and call handler
        };
        
        function registerPath(path, onRegisterResponseHandler) {
        }
                  
      revHttpProxies[revHttpProxyUri] = newProxyConnection;
      return newProxyConnection;
          };
          function unregister(path, registrationKey, onUnregisterHandler) {};
          function request(path, registrationKey, onRequestResponseHandler, clientRequestHandler) {};
          function unrequest(path, registrationKey, onUnrequestResponseHandler) {};
          
          return {
            register : register,
            unregister : unregister,
            request : request,
            unrequest : unrequest
          };
      }();

    }
  }
  
  return connect;
  
}();





var revHttp = RevHttp("uri");
revHttp.onConnect = function()
revHttp.
revHttp.registerPath(handler)
revHttp.registerHandler(path, registrationKey, handler);
revHttp.unregisterHandler(path, regKey);

revHttp.connect("uri", function(result, connection) {
  if (result.success) {
    connection.register('path') {
    }
  }
});
object.register()

...


1 client 1 path OK
1 client 2 paths OK
2 clients 1 path NOT OK

- client connects
- client registers handler for path. that path cannot be registered again until client gets up again
- client registers a path and receives a key for it. path isnt deleted until unregister is called - cool uris dont change


